#include "../../externals/PixelToaster.h"
#include "../math/basics.h"
#include "../math/transformation.h"
#include <Windows.h>

using namespace PixelToaster;
float fov = 45.0f;
const unsigned int kNumSurfaces = 5;

// =================================================================================================
// Entry point into the ray tracer application
// =================================================================================================
int main() {

    float width = 800.0f;
    float height = 600.0f;

	float radfov = fov * 3.14159265f/180.0f;  

	float depth = -cos(radfov)/sin(radfov);

    Display display("Ray Tracer Example", int(width), int(height));

	// -- pixel buffer
    vector<Pixel> pixels(int(width * height));

	for(unsigned int i = 0; i < pixels.size(); ++i) {
		pixels[i].r = 0.0f;
		pixels[i].g = 0.0f;
		pixels[i].b = 0.0f;
	}
	// -- depth buffer
	vector<float> buffer(int(width * height));

	for(unsigned int i = 0; i < buffer.size(); ++i) 
		buffer[i] = 3.0f;

	Vector4 origin1 = {1.0f, 0.0f, 0.0f};
	Vector4 normal1 = {-1.0f, 0.0f, 0.0f};
	Color col1 = {1.0f, 0.0f, 0.0f};
	Plane plane1 = {origin1, normal1, col1 };
	Vector4 origin2 = {-1.0f, 0.0f, 0.0f};
	Vector4 normal2 = {1.0f, 0.0f, 0.0f};
	Color col2 = {0.0f, 1.0f, 0.0f};
	Plane plane2 = {origin2, normal2, col2 };
	Vector4 origin3 = {0.0f, 1.0f, 0.0f};
	Vector4 normal3 = {0.0f, -1.0f, 0.0f};
	Color col3 = {0.0f, 0.0f, 1.0f};
	Plane plane3 = {origin3, normal3, col3};
	Vector4 origin4 = {0.0f, -1.0f, 0.0f};
	Vector4 normal4 = {0.0f, 1.0f, 0.0f};
	Color col4 = {0.5f, 0.5f, 0.5f};
	Plane plane4 = {origin4, normal4, col4};
	Vector4 origin5 = {0.0f, 0.0f, -2.0f};
	Vector4 normal5 = {0.0f, 0.0f, 1.0f};
	Color col5 = {0.0f, 0.5f, 0.5f};
	Plane plane5 = {origin5, normal5, col5};

	Plane planes[kNumSurfaces];
	planes[0] = plane1;
    planes[1] = plane2;
	planes[2] = plane3;
	planes[3] = plane4;
	planes[4] = plane5;

    while (display.open()) {
        unsigned int index = 0;

		if(GetAsyncKeyState(VK_LEFT) != 0) {
			Matrix4x4& ident = Identity();
			RotateY(0.03f, ident);

			for(unsigned int i = 0; i < kNumSurfaces; ++i)
				planes[i].normal *= ident; 
		}

		if(GetAsyncKeyState(VK_RIGHT) != 0) {
			Matrix4x4& ident = Identity();
			RotateY(-0.03f, ident);

			for(unsigned int i = 0; i < kNumSurfaces; ++i)
				planes[i].normal *= ident;
		}

        for (unsigned int y = 0; y < height; ++y) {
            for (unsigned int x = 0; x < width; ++x) {
				for(unsigned int i = 0; i < kNumSurfaces; ++i) 
	                SSTransformation(x, y, depth, planes[i], width, height, 
						             buffer[index], kNumSurfaces, pixels[index]);
				++index;
            }
        }
		display.update(pixels);
		for(unsigned int i = 0; i < buffer.size(); ++i) {
			buffer[i] = 3.0f;
		    pixels[i].r = 0.0f;
			pixels[i].g = 0.0f;
			pixels[i].b = 0.0f;
		}
	
    }

	return 0;
}

